<?php
	class  CategoryController extends PlatformController
	{
		/**
		 * 显示分页管理控制器
		 */
		public function indexAction()
		{
			$category=Factory::M('CategoryModel');
			$cateInfo=$category->getCategory();
			$this->assign('cateInfo',$cateInfo);
			$this->display('index.html');
		}
		public function addAction()
		{
		
			$category=Factory::M('CategoryModel');
			$cateInfo=$category->getCategory();
			$this->assign('cateInfo',$cateInfo);
			$this->display('add.html');
		}
		public function editAction()
		{
			$cate_id=$_GET['cate_id'];
			$category=Factory::M('CategoryModel');
			// $cate=$category->editCateogry();
			$cate=$category->getCategoryById($cate_id);
			$this->assign('cate',$cate);
			$cateInfo=$category->getCategory();
			$this->assign('cateInfo',$cateInfo);
			$this->display('edit.html');
		}
		public function delAction()
		{
			$cate_id=$_GET['cate_id'];
			$category=Factory::M('CategoryModel');
			$subId=$category->getSubId($cate_id);
			if($subId)
			{
			 $this->jump('index.php?p=Back&c=Category&a=index','存在子分类,删除失败');
			}
			$result=$category->delCategoryById($cate_id);
			if ($result) 
			{
				$this->jump('index.php?p=Back&c=Category&a=index');
			}
			else 
			{
				$this->jump('index.php?p=Back&c=Category&a=index','未知错误,删除失败');
			}

		}
		public function delAllAction()
		{
			if(!isset($_POST['cate_id']))
			{
				$this->jump('index.php?p=Back&c=Category&a=index',':(未选中删除项');
			}
			$cate_id=$_POST['cate_id'];
			
			$category=Factory::M('CategoryModel');
			foreach ($cate_id as $id)
			{
				if($category->getSubId($id))
				{
					$this->jump('index.php?p=Back&c=Category&a=index','存在子分类,删除失败');
				}
			}
			$result=$category->delAllCategory($cate_id);
			// var_dump($result);die;
			if ($result) 
			{
				$this->jump('index.php?p=Back&c=Category&a=index');
			}
			else 
			{
				$this->jump('index.php?p=Back&c=Category&a=index','未知错误,删除失败');
			}

		}
		public function dealEditAction()
		{
			//接受数据
			$cate=array();
			$cate['cate_name']=$this->escapeData($_POST['cate_name']);
			$cate['cate_pid']=$_POST['cate_pid'];
			$cate['cate_sort']=$this->escapeData($_POST['cate_sort']);
			$cate['cate_desc']=$this->escapeData($_POST['cate_desc']);
			$cate['cate_id']=$_POST['cate_id'];
			//判断数据合法性
			if(empty($cate['cate_name']) || empty($cate['cate_desc']) || empty($cate['cate_sort']))
			{
				$this->jump("index.php?p=Back&c=Category&a=edit&cate_id={$cate['cate_id']}",':(信息不完整!');
			}
			if(!is_numeric($cate['cate_sort']) || (int)$cate['cate_sort'] != $cate['cate_sort'] || $cate['cate_sort']<0)
			{
				$this->jump("index.php?p=Back&c=Category&a=edit&cate_id={$cate['cate_id']}",'排序应该为1-100');
			}
			//数据入库,操作模型
			$category=Factory::M('CategoryModel');
			//插入数据
			$result=$category->updateCateById($cate);
			//跳转
			if($result == 0)
			{
				$this->jump('index.php?p=Back&c=Category&a=index');
//echo  <<<TZ
// 				<script type="text/javascript">
// 				return confirm('不修改内容?');
// 				</script>
// TZ;		
// echo <<<TIAOZHUAN
// <script type="text/javascript">
// 				confirm('不修改内容?');
// 				</script>
// TIAOZHUAN;
			}
			if($result)
			{
				$this->jump('index.php?p=Back&c=Category&a=index');
			}
			else
			{
				$this->jump("index.php?p=Back&c=Category&a=edit&cate_id={$cate['cate_id']}",':(未知错误,修改失败');
			}
		}
		public function dealAddAction()
		{
			//接受数据
			$cate=array();
			$cate['cate_name']=$this->escapeData($_POST['cate_name']);
			$cate['cate_pid']=$_POST['cate_pid'];
			$cate['cate_sort']=$this->escapeData($_POST['cate_sort']);
			$cate['cate_desc']=$this->escapeData($_POST['cate_desc']);
			
		
			//判断数据合法性
			if(empty($cate['cate_name']) || empty($cate['cate_desc']) || empty($cate['cate_sort']))
			{
				$this->jump('index.php?p=Back&c=Category&a=add',':(信息不完整!');
			}
			if(!is_numeric($cate['cate_sort']) || (int)$cate['cate_sort'] != $cate['cate_sort'] || $cate['cate_sort']<1)
			{
				$this->jump('index.php?p=Back&c=Category&a=add','排序应该为1-100');
			}
			//数据入库,操作模型
			$category=Factory::M('CategoryModel');
			//插入数据
			$result=$category->insertCate($cate);
			//跳转		
			
			if($result)
			{
				$this->jump('index.php?p=Back&c=Category&a=index');
			}
			else
			{
				$this->jump('index.php?p=Back&c=Category&a=add',':(未知错误,添加失败');
			}
		}

	}